class User < ActiveRecord::Base
  
  validates_uniqueness_of :email,:allow_blank => true,:message => "此Email已经存在!"
  validates_uniqueness_of :employee_id,:allow_blank => true,:message => "此员工号已经存在!"
  
  belongs_to :department
  has_many :projectusers
  has_many :projects ,:through => :projectusers,:order => "id DESC"	
  
  has_many :productusers,:include =>"product",:order => "productusers.id DESC"
  has_many :products ,:through => :productusers,:order => "id"
  
	 
  def pinyin_name1
    [last_name,first_name].join(" ")
  end 
	
  def pinyin_name2
    [first_name,last_name].join(" ")
  end 
	
  def self.per_page
    20
  end

  def self.search(page)
      
      paginate :per_page => 20,:page =>page,
                :include =>[:department],:order => "last_name asc,first_name asc"
  end
 	 
  def self.search2(search,page)
    paginate :per_page => 10000,:page =>page,
      :conditions => ['employee_id like ? or last_name like ? or first_name like ? or english_name like ? or chinese_name like ?
 	 		or ext like ? or email like ?',"%#{search}%","%#{search}%","%#{search}%","%#{search}%","%#{search}%","%#{search}%","%#{search}%"],
      :include =>[:department],:order => "last_name asc"
  end
         
  def self.search3(search)
      
      User.find :all,
      :conditions => ['concat(last_name,first_name,english_name,chinese_name,email,pinyin,employee_id,ext) like ? ',"%#{search}%"],
      :include =>[:department],:order => "last_name asc"
  end
  
   def self.search4(search)

       User.find :all,
       :conditions => ['employee_id like ? or last_name like ? or first_name like ? or english_name like ? or chinese_name like ?
  	 		or ext like ? or email like ? or pinyin like ?',"%#{search}%","%#{search}%","%#{search}%","%#{search}%","%#{search}%","%#{search}%","%#{search}%","%#{search}%"],
       :include =>[:department],:order => "last_name asc,first_name asc"
   end

end
